package com.gitee.mazhenggg.demo.auth.controller;

import com.gitee.mazhenggg.demo.auth.security.SecurityUserDetails;
import com.gitee.mazhenggg.demo.auth.service.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private JwtService jwtUtil;

    @GetMapping(value = "/verify")
    public String verifyToken(Authentication auth) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        if (auth.isAuthenticated()) {
            SecurityUserDetails securityUserDetails = (SecurityUserDetails) auth.getPrincipal();
            String jwtToken = jwtUtil.generateToken(securityUserDetails);
            return jwtToken;
        } else {
            return "TOKEN_INVALID";
        }
    }


}